<@app.html>
    <@app.head>
    <script>
        var currentCheck = new Array();
        var totalCheck = new Array();
        $.extend($.fn.validatebox.defaults.rules, {
            unique: {
                validator: function (value) {
                    var success;
                    $.ajax({
                        cache: false,
                        async: false,
                        type: 'POST',
                        url: '${contextPath}/security/hdw/cabinet/unique.htm',
                        data: {
                            id: value
                        },
                        dataType: 'json',
                        success: function (json) {
                            <@app.json_jump/>
                            if (json.success) {
                                success = true;
                            } else {
                                success = false;
                            }
                        }
                    });

                    return success;
                },
                message: '终端编号重复'
            }
        });
        $(function () {
            $('#page_table').datagrid({
                fit: true,
                width: '100%',
                height: '100%',
                striped: true,
                pagination: true,
                url: "${contextPath}/security/hdw/cabinet/page.htm",
                fitColumns: true,
                pageSize: 10,
                pageList: [10, 50, 100],
                idField: 'id',
                singleSelect: true,
                selectOnCheck: false,
                checkOnSelect: false,
                autoRowHeight: false,
                rowStyler: gridRowStyler,
                columns: [
                    [
                        {
                            field: 'checkbox',
                            checkbox: true
                        },
                        {
                            title: '终端编号',
                            align: 'center',
                            field: '1',
                            width: 40,
                            formatter: function (val, row) {
                                return row.id;
                            }
                        },
                        {
                            title: '名称',
                            align: 'center',
                            field: 'cabinetName',
                            width: 40
                        },
                        {
                            title: '地址',
                            align: 'center',
                            field: 'address',
                            width: 60,
                            formatter: function (val, row) {
                                return (row.provinceName || '') + (row.cityName || '') + (row.districtName || '') + (row.street || '');
                            }
                        },
                        {
                            title: '状态',
                            align: 'center',
                            field: 'activeFlag',
                            width: 20,
                            formatter: function (val, row) {
                                return val == 1 ? '启用' : '禁用'
                            }
                        },
                        {
                            title: '在线',
                            align: 'center',
                            field: 'onlineFlag',
                            width: 20,
                            formatter: function (val, row) {
                                return val == 1 ? '是' : '否'
                            }
                        },
                        {
                            title: '运营商',
                            align: 'center',
                            field: 'agentName',
                            width: 60
                        },

                        {
                            title: '代理商',
                            align: 'center',
                            field: 'agencyName',
                            width: 60
                        },
                        // {
                        //     title: '最后连接',
                        //     align: 'center',
                        //     field: 'heartTime',
                        //     width: 60
                        // },


                        {
                            title: '操作',
                            align: 'center',
                            field: 'id',
                            width: 80,
                            formatter: function (val, row) {
                                var html = '<a href="javascript:view(\'ID\')">查看</a>';
                                    /*html += ' <a href="javascript:downloadLog(\'ID\')">下载终端日志</a>'*/
                                <@app.has_oper oper_code='2_1_3'>
                                    html += ' <a href="javascript:edit(\'ID\')">修改</a>'
                                </@app.has_oper>
                                <@app.has_oper oper_code='2_1_4'>
                                    html += ' <a href="javascript:remove(\'ID\')">删除</a>'
                                </@app.has_oper>
                                <@app.has_oper oper_code='2_1_5'>
                                    html += ' <a href="javascript:usageLog(\'ID\')">使用记录</a>'
                                </@app.has_oper>
                                return html.replace(/ID/g, row.id);
                            }
                        }
                    ]
                ],
                onLoadSuccess: function () {
                    $('#page_table').datagrid('clearChecked');
                    $('#page_table').datagrid('clearSelections');
                    //当前页面存在与总数组的数据选中 并在总数组清除  添加到当前页面数组
                    var rows = $('#page_table').datagrid('getData').rows;
                    for (var i = 0, l = rows.length; i < l; i++) {
                        if ($.inArray(rows[i].id, totalCheck) != -1) {
                            $("#page_table").datagrid("checkRow", i);
                            currentCheck.push(rows[i].id);
                            totalCheck.splice($.inArray(rows[i].id, totalCheck), 1);
                        }
                    }
                },
                onBeforeLoad: function () {
                    //当前页面数组和总数组不重复的添加到总数组
                    for (var i = 0; i < currentCheck.length; i++) {
                        if ($.inArray(currentCheck[i], totalCheck) == -1) {
                            totalCheck.push(currentCheck[i])
                        }
                    }
                },
                //单选
                onCheck: function () {
                    currentCheck.length = 0;
                    for (var i = 0; i < $('#page_table').datagrid('getChecked').length; i++) {
                        currentCheck.push($('#page_table').datagrid('getChecked')[i].id)
                    }
                },
                //全选
                onCheckAll: function () {
                    currentCheck.length = 0;
                    for (var i = 0; i < $('#page_table').datagrid('getChecked').length; i++) {
                        currentCheck.push($('#page_table').datagrid('getChecked')[i].id)
                    }
                },
                //取消单选
                onUncheck: function () {
                    currentCheck.length = 0;
                    for (var i = 0; i < $('#page_table').datagrid('getChecked').length; i++) {
                        currentCheck.push($('#page_table').datagrid('getChecked')[i].id)
                    }
                },
                //取消所有
                onUncheckAll: function () {
                    currentCheck.length = 0;
                }
            })
        })
        function reload() {
            var datagrid = $('#page_table');
            datagrid.datagrid('reload');
        }

        function query() {
            totalCheck.length = 0;
            var tree = $('#area_tree');
            var datagrid = $('#page_table');

            var id = $('#id').val();
            var cabinetName = $('#cabinet_name').val();

            var agentId = $('input[name="agentId"]').val();
            var agencyId = $('input[name="agencyId"]').val();
            var queryParams = {
                id: id,
                cabinetName: cabinetName,
                agentId: agentId,
                agencyId: agencyId
            };

            datagrid.datagrid('options').queryParams = queryParams;

            datagrid.datagrid('load');
        }
        function add() {
            App.dialog.show({
                css: 'width:650px;height:390px;overflow:visible;',
                title: '新建',
                href: "${contextPath}/security/hdw/cabinet/add.htm",
                event: {
                    onClose: function () {
                        var datagrid = $('#page_table');
                        datagrid.datagrid('reload');
                    },
                    onLoad: function () {
                    }
                }
            });
        }

        function usageLog(id) {
            App.dialog.show({
                css: 'width:953px;height:540px;',
                title: '使用记录',
                href: "${contextPath}/security/hdw/cabinet/usageLog.htm?id=" + id
            });
        }

        function downloadLog(id) {
            App.dialog.show({
                css: 'width:295px;height:195px;',
                title: '下载终端日志',
                href: "${contextPath}/security/hdw/cabinet/downloadLog.htm?id=" + id
            });
        }
        function edit(id) {
            App.dialog.show({
                css: 'width:830px;height:515px;overflow:visible;',
                title: '修改',
                href: "${contextPath}/security/hdw/cabinet/edit.htm?id=" + id,
                event: {
                    onClose: function () {
                        var datagrid = $('#page_table');
                        datagrid.datagrid('reload');
                    },
                    onLoad: function () {
                    }
                }
            });
        }
        function editTemperature() {
            var cabinetId = new Array();
            for (var i = 0; i < currentCheck.length; i++) {
                if ($.inArray(currentCheck[i], totalCheck) == -1) {
                    cabinetId.push(currentCheck[i])
                }
            }
            for (var i = 0; i < totalCheck.length; i++) {
                cabinetId.push(totalCheck[i])
            }
            if (cabinetId.length > 0) {
                console.log(cabinetId)
                App.dialog.show({
                    css: 'width:284px;height:176px;overflow:visible;',
                    title: '修改',
                    href: "${contextPath}/security/hdw/cabinet/edit_temperature.htm?cabinetId=" + cabinetId
                });
            } else {
                alert("未选择终端");
            }
        }
        function view(id) {
            App.dialog.show({
                css: 'width:830px;height:515px;overflow:visible;',
                title: '查看',
                href: "${contextPath}/security/hdw/cabinet/view.htm?id=" + id
            });
        }

        function remove(id) {
            $.messager.confirm('提示信息', '确认删除?', function (ok) {
                if (ok) {
                    $.post('${contextPath}/security/hdw/cabinet/delete.htm', {
                        id: id
                    }, function (json) {
                        if (json.success) {
                            $.messager.alert('info', '操作成功', 'info');
                            reload();
                        } else {
                            $.messager.alert('提示消息', json.message, 'info');
                        }
                    }, 'json');
                }
            });
        }
    </script>
    </@app.head>
    <@app.body>
        <@app.container>
            <@app.banner/>
        <div class="main">
            <@app.menu/>
            <div class="content">
                <div class="panel search">
                    <div class="float_right">
                        <button class="btn btn_yellow" onclick="query()">搜索</button>
                    </div>
                    <table cellpadding="0" cellspacing="0" border="0">
                        <tr>
                            <#if Session['SESSION_KEY_USER'].type == Session['USER_TYPE_PLATFORM'].value>
                                <td align="right">运营商：</td>
                                <td>
                                    <input name="agentId" class="easyui-combotree" editable="false"
                                           style="width: 184px; height: 28px;"
                                           data-options="url:'${contextPath}/security/basic/agent/tree.htm?dummy=${'所有'?url}',method:'get',onSelect: function(rec){
                               var url = '${contextPath}/security/hdw/agency/tree.htm?dummy=${'所有'?url}&agentId='+rec.id;
                               $('#agency_id').combotree('reload', url);},
                               onChange:function(){
                               $('#agency_id').combotree('clear');}"/>
                                </td>
                            </#if>
                            <#if Session['SESSION_KEY_USER'].type == Session['USER_TYPE_AGENT'].value||Session['SESSION_KEY_USER'].type == Session['USER_TYPE_PLATFORM'].value>
                                <td align="right">代理商：</td>
                                <td>
                                    <input name="agencyId" class="easyui-combotree" editable="false" id="agency_id"
                                           data-options="url:'${contextPath}/security/hdw/agency/tree.htm?dummy=${'所有'?url}&agentId=${Session['SESSION_KEY_USER'].agentId}'"
                                           style="width: 184px; height: 28px;"/>
                                </td>
                            </#if>
                            <td align="right" width="60">终端编号：</td>
                            <td><input type="text" class="text" id="id"/></td>
                            <td align="right" width="60">名称：</td>
                            <td><input type="text" class="text" id="cabinet_name"/></td>
                        </tr>
                    </table>
                </div>
                <div class="panel grid_wrap">
                    <div class="toolbar clearfix">
                        <div class="float_right">
                            <@app.has_oper oper_code='2_1_2'>
                                <button class="btn btn_green" onclick="add()">新建</button>
                            </@app.has_oper>
                            <@app.has_oper oper_code='2_1_6'>
                                <a href="${contextPath}/security/basic/baidu_map/cabinet_twinkle_location.htm" target="_blank">
                                    <button class="btn btn_blue">分布图</button>
                                </a>
                            </@app.has_oper>
                            <@app.has_oper oper_code='2_1_3'>
                                <button class="btn btn_red" onclick="editTemperature()">批量修改温度</button>
                            </@app.has_oper>
                        </div>
                        <h3>终端列表</h3>
                    </div>
                    <div class="grid">
                        <table id="page_table"></table>
                    </div>
                </div>
            </div>
        </div>
        </@app.container>
    </@app.body>
</@app.html>

